package com.xzkydz.all_sensor.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.serialport.ComAssistant.Java_xor;
import com.xzkydz.all_sensor.activity.MainActivity;
import com.xzkydz.all_sensor.allsensor.R;

/**
 * Created by Smilekun on 2016/8/19.
 */
public class SettingFlowSensorFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View  view  =  inflater.inflate(R.layout.fragment_setting_flow_sensor, container, false);
        final MainActivity mActivity  = (MainActivity)getActivity();

        Button sendButton  =   (Button)view.findViewById(R.id.btn_send_inf);  //发送按钮
        final EditText  gdwjEdittext  =  (EditText)view.findViewById(R.id.et_guandao_waijing);  //管道外径
        final EditText  gdhdEdittext  =  (EditText)view.findViewById(R.id.et_guandao_houdu);  //管壁厚度
        final Spinner   caizhiSpinner  =  (Spinner)view.findViewById(R.id.sp_guandaocaizhi);//材质
        final Spinner  chaunganqiAnzhuangfashiSinner  = (Spinner)view.findViewById(R.id.sp_chuanganqianzhuangfangshi);//传感器安装方式
        final Spinner   liutiSpinner  =  (Spinner)view.findViewById(R.id.sp_liutileixing); //流体类型

        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (gdhdEdittext.length()!=0 && gdwjEdittext.length()!=0) {
                    String showInf = "";
                    showInf = "    管道外径 ：" + gdwjEdittext.getText().toString() + " mm     管壁厚度：" + gdhdEdittext.getText().toString()+"mm\n";
                    showInf = showInf+ "    管道材质 ：" + caizhiSpinner.getSelectedItem().toString()+"        传感器安装方式："+chaunganqiAnzhuangfashiSinner.getSelectedItem().toString()+"\n";
                    showInf = showInf+"    流体类型 ："+liutiSpinner.getSelectedItem().toString()+"\n";


                    String  azhuangfangfaString  = "0"+chaunganqiAnzhuangfashiSinner.getSelectedItemId();
                    String  caizhiString  =  "0"+caizhiSpinner.getSelectedItemId()+""; //材质
                    String  waijingString  = Integer.toHexString(Integer.parseInt(gdwjEdittext.getText().toString()) );
                    String  houduString  = Integer.toHexString(100*Integer.parseInt(gdhdEdittext.getText().toString()) );
                    if (houduString.length()==1) houduString = "0"+houduString;
                    if (waijingString.length()==1) waijingString = "0"+waijingString;
                    String  liuyiString  = "";
                    if (liutiSpinner.getSelectedItemId()<10)   liuyiString  =  "0"+liutiSpinner.getSelectedItemId();
                    else  liuyiString = liutiSpinner.getSelectedItemId()+"";

                    if (waijingString.length() != 4) {
                        for (int i = waijingString.length(); i < 4; i++) {
                            waijingString = "0" + waijingString;
                        }
                    }
                    if (houduString.length() != 4) {
                        for (int i = houduString.length(); i < 4; i++) {
                            houduString = "0" + houduString;
                        }
                    }

                    String   completeData  =  "4B590E01050001A00102"+azhuangfangfaString+caizhiString+liuyiString+waijingString+houduString;
                    String xorString = Java_xor.checkcode_0007("0E01050001A00102" + azhuangfangfaString+caizhiString+liuyiString+waijingString+houduString);

                    completeData  =  completeData +xorString;  //完整的包
                    mActivity.ShowInf(showInf+"\n"+completeData+"\n\n\n");
                    mActivity.sendPortData(completeData);  //发送数据
                }else{
                    Toast.makeText(mActivity,"禁止： 管道外径、管壁厚度为空...",Toast.LENGTH_SHORT).show();
                }

            }
        });
            return view;
    }
}
